WorkerScript.onMessage = function(message) {
    doLoadImages(message);
}


function doLoadImages(message) {
    var xhr = new XMLHttpRequest;
    //https://production-2.sogeoapi.com/api/poi/:poi_id/media
    var url = message.g.serverAPI + "poi/" + message.spotId + "/media?page=1&per_page=" + message.g.perPage + "&oauth_token=" + message.g.token;

    xhr.open("GET", url);
    xhr.onreadystatechange = function() {
        try {
            if (xhr.readyState == XMLHttpRequest.DONE) {
                message.model.clear();
                //console.log(xhr.responseText)
                var response = JSON.parse(xhr.responseText);
                for (var i in response.data) {
                    var image = response.data[i].media;

                    message.model.append({
                                  imageId: image.id,
                                  imageThumb: image.thumb_pic,
                                  imageUrl: image.url

                    });
                }
                message.model.sync();
                WorkerScript.sendMessage({ 'result': 'SUCCESS' })
            }
        } catch(err) {
            //console.log(err)
            WorkerScript.sendMessage({ 'result': 'FAILURE', 'message': err + ", " + xhr.responseText });
        }

    }
    xhr.send();
}
